package simpleSpring.mybatis;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

/**
 * @author caihe
 * @date 2022/10/26 11:41
 */
public class CaiheBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        // 扫描
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(CaiheScan.class.getName());
        String path = (String) annotationAttributes.get("value");
        // 找路径下有多少mapper

        CaiheScanner caiheScanner = new CaiheScanner(registry);
        caiheScanner.addIncludeFilter((metadataReader, metadataReaderFactory)-> true);
        caiheScanner.scan(path);
        

//        // 生成一个空的beanDefinition
//        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
//        beanDefinition.setBeanClass(CaiheFactoryBean.class);
//        beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(UserDao.class);
//        registry.registerBeanDefinition("userDao", beanDefinition);
//
//        AbstractBeanDefinition orderDaoBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
//        orderDaoBeanDefinition.setBeanClass(CaiheFactoryBean.class);
//        orderDaoBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue(OrderDao.class);
//        registry.registerBeanDefinition("orderDao", orderDaoBeanDefinition);
        
//        ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry);
    }
}
